package com.company.bookstore.validator.user_admin_module;

import com.company.bookstore.entities.user_admin_module.User;
import com.company.bookstore.exception.user_admin_module.CustomException;
import com.company.bookstore.qo.user_admin_module.AddressQo;
import com.company.bookstore.service.user_admin_module.UserService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.regex.Pattern;

@Component
public class AddressRegisterValidator extends AbstractValidatorHandler implements InitializingBean {
    @Resource
    UserService userService;
    static final Pattern NAME_PATTERN = Pattern.compile("^[\\u4e00-\\u9fa5a-zA-Z]{0,20}");
    @Override
    public void afterPropertiesSet() throws Exception {
        ValidatorFactory.register(this.getClass().getName(),this);
    }
    @Override
    public boolean doValidator(Object object) throws NoSuchMethodException, CustomException {

        AddressQo addressQo = (AddressQo)object;
        if (addressQo.getId() != null){
            throw  new CustomException("参数传输错误，不可指定用户",405);
        }

        String city = addressQo.getCity();
        String town = addressQo.getTown();
        String country = addressQo.getCountry();
        Integer deleted = addressQo.getDeleted();
        String detail = addressQo.getDetail();
        String phone = addressQo.getPhone();
        String province = addressQo.getProvince();
        String username = addressQo.getUsername();
        Integer fkUserId = addressQo.getFkUserId();

        if (username!=null){
            if (! NAME_PATTERN.matcher(username).matches()){
                throw  new CustomException("性名格式不正确，要求0-20位中文，字母，不能含有标点符号,数字",405);
            }
        }else{
            throw  new CustomException("没有收货人信息",405);
        }
        if (!StringUtils.isEmpty(city)){
            if (! AddressQoUpdateValidator.CITY_PATTERN.matcher(city).matches()){
                throw  new CustomException("城市名称不正确，要求0-20位中文，字母，数字，不能含有标点符号",405);
            }
        }else {
            throw  new CustomException("没有城市信息",405);
        }
        if (!StringUtils.isEmpty(town)){
            if (! AddressQoUpdateValidator.TOWN_PATTERN.matcher(town).matches()){
                throw  new CustomException("城镇名称不正确，要求0-20位中文，字母，数字，不能含有标点符号",405);
            }
        }else {
            throw  new CustomException("没有乡镇信息",405);
        }
        if (!StringUtils.isEmpty(country)){
            if (! AddressQoUpdateValidator.COUNTRY_PATTERN.matcher(country).matches()){
                throw  new CustomException("国家名称不正确，要求0-20位中文，字母，数字，不能含有标点符号",405);
            }
        }else {
            throw  new CustomException("没有国家信息",405);
        }
        if (!StringUtils.isEmpty(province)){
            if (! AddressQoUpdateValidator.PROVINCE_PATTERN.matcher(province).matches()){
                throw  new CustomException("国家名称不正确，要求0-20位中文，字母，数字，不能含有标点符号",405);
            }
        }else {
            throw  new CustomException("没有省份信息",405);
        }
        if (!StringUtils.isEmpty(phone)){
            if (! UserQoUpdateRegisterValidator.USER_PHONE_PATTERN.matcher(phone).matches()){
                throw  new CustomException("账号手机号不正确，要求数字11到12位",405);
            }
        }else {
            throw  new CustomException("没有联系方式信息",405);
        }
        if (!StringUtils.isEmpty(detail)){
            if (! AddressQoUpdateValidator.DETAIL_PATTERN.matcher(detail).matches()){
                throw  new CustomException("详细地址不正确，要求不允许有特殊标点符号，可以使用-_",405);
            }
        }else {
            throw  new CustomException("没有详细地址信息",405);
        }
        if (deleted!=null){
            throw new CustomException("不能指定删除状态字段",405);
        }
        if (fkUserId != null){
            User byId = userService.getById(fkUserId);
            if (byId == null){
                throw new CustomException("没有该用户",405);
            }
        }else {
            throw new CustomException("没有指定用户",405);
        }

        return true;
    }
}
